/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.grunner.dialog

import de.kieselbach.swing.*
import de.kieselbach.swing.border.*
import java.awt.*
import java.util.logging.*
import javax.swing.*

class AboutDialog {

    def dialog

    AboutDialog(def mainframe) {

        def area = new JTextArea()
        area.editable = false
        area.opaque = false
        area.border = BorderFactory.createCompoundBorder(CustomLineBorder.create(UIUtilities.lineBorderColor, '78963214'), BorderFactory.createEmptyBorder(5, 5, 5, 5))

        def reader = null
        try {
            reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(mainframe.resourceBundle.getString('about.text.uri')), 'UTF-8'))
            def str = reader.readLine()
            while (str != null) {
                area.append(str)
                str = reader.readLine()
                if (str != null) {
                    area.append('\n')
                }
            }
        }
        catch (Exception ex) {
            Logger.getLogger(getClass().name).log(Level.WARNING, 'Unable to load the about text!', ex)
        }
        finally {
            if (reader != null) {
                reader.close()
            }
        }

        def button = null
        def panebuilder = new PaneBuilder()
        dialog = panebuilder.dialog(mainframe.resourceBundle.getString('about.title'), owner: mainframe.frame, modal: true, resizable: false, pack: true, center: true) {
            pane() {
                widget(area)
                bar(type: PaneBuilder.DEFAULT)  {
                    button = close(default: true, actionPerformed: {
                        dialog.visible = false
                    })
                }
            }
        }
        button.requestFocus()
    }

    void show() {
        dialog.setVisible(true)
        dialog.dispose()
    }
}

